package com.shadow.ai.base;

import dev.langchain4j.data.message.AiMessage;
import dev.langchain4j.data.message.SystemMessage;
import dev.langchain4j.data.message.UserMessage;
import dev.langchain4j.model.chat.ChatModel;
import dev.langchain4j.model.chat.response.ChatResponse;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

@Slf4j
@Service
public class AiCodeHelper {

    private static final String SYSTEM_MSG = """
                你是编程领域的小助手，帮助用户解答编程学习和求职面试相关的问题，并给出建议。
                重点关注 4 个方向:
                1.规划清晰的编程学习路线
                2.提供项目学习建议
                3.给出程序员求职全流程指南(比如简历优化、投递技巧)
                4.分享高频面试题和面试技巧请用简洁易懂的语言回答，助力用户高效学习与求职。
            """;

    @Resource
    private ChatModel chatModel;

    public String chat(final String message) {
        SystemMessage systemMessage = SystemMessage.from(SYSTEM_MSG);
        UserMessage userMessage = UserMessage.from(message);
        ChatResponse chatResponse = chatModel.chat(systemMessage, userMessage);
        AiMessage aiMessage = chatResponse.aiMessage();
        log.info("AI Message: {}", aiMessage);
        return aiMessage.text();
    }

    public String chatWhitMessage(final UserMessage userMessage) {
        ChatResponse chatResponse = chatModel.chat(userMessage);
        AiMessage aiMessage = chatResponse.aiMessage();
        log.info("AI Message: {}", aiMessage);
        return aiMessage.text();
    }
}
